//
//  SDCompareDetectionVC.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/31.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDCompareDetectionVC.h"
#import "SDCompareDetecSelectReportVC.h"
#import "SDCompareDetcetionCell.h"
#import "SDCompareDetectionHeaderView.h"
#import "SDCompareDetectionCellModel.h"

#import "SDMyReportData.h"
#import "SDDetectionReportData.h"
#import "SDDetectionReportModel.h"


static NSString *const cellIden = @"SDCompareDetcetionCell";
static NSString *const headerIden = @"SDCompareDetectionHeaderView";

@interface SDCompareDetectionVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)SDCompareDetectionCellModel *model0;//第一个报告

@property (nonatomic ,strong)SDCompareDetectionCellModel *model1;//第二个报告

@property (nonatomic ,strong)SDCompareDetecSelectReportVC *selectReport;

@property (nonatomic ,assign)SDCompareDetectionCellModel *modelSelect;

@end

@implementation SDCompareDetectionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
     _navHeight.constant = SDNavBar_Height;
    [self.tableView registerNib:[UINib nibWithNibName:cellIden bundle:nil] forCellReuseIdentifier:cellIden];
    
    [self.tableView registerNib:[UINib nibWithNibName:headerIden bundle:nil] forHeaderFooterViewReuseIdentifier:headerIden];
   
    [self addSelectReportVC];
    
    //如果用户的报告数量小于二那么第一个报告和第二个报告一样
    if (self.reportAllArray.count < 2) {
        
        SDMyReportData *data = self.reportAllArray.firstObject;
        [self getReportinfoWithReportId:[data.id intValue] block:^(SDDetectionReportData *data) {
            self.model0.data = self.model1.data = data;
             self.model0.skinImage = self.model1.skinImage = data.resImg1;
            [self.tableView reloadData];
        }];
        
        
        
    }else{
        
        dispatch_group_t group = dispatch_group_create();
        SDMyReportData *data = self.reportAllArray.firstObject;
        dispatch_group_enter(group);
        //获取第一个报告
        [self getReportinfoWithReportId:[data.id intValue] block:^(SDDetectionReportData *data) {
            self.model0.data =  data;
            self.model0.skinImage = data.resImg1;
            dispatch_group_leave(group);
            
        }];
        
        dispatch_group_enter(group);
        SDMyReportData *data2 = self.reportAllArray[1];
        //获取第二个报告
        [self getReportinfoWithReportId:[data2.id intValue] block:^(SDDetectionReportData *data) {
           self.model1.data = data;
           self.model1.skinImage = data.resImg1;
            dispatch_group_leave(group);
           
        }];
        //回主线程刷新页面
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    }
    
}

/**
 获取检测报告详情

 @param reportId 报告ID
 @param success 获取数据成功后回调
 */
- (void)getReportinfoWithReportId:(int)reportId block:(void(^)(SDDetectionReportData *data))success
{
    
    [SDNetwork getReportinfoWithReportId:reportId Finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
        
        if (!error) {
            
            SDDetectionReportModel *showModel = [SDDetectionReportModel modelObjectWithDictionary:dic];
            success(showModel.data);
            [self.tableView reloadData];
            
        }
        
    }];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 230 * SDWidth375Scale;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    SDCompareDetcetionCell*cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
    SDCompareDetectionCellModel *model = indexPath.section == 0? self.model0:self.model1;
    [cell.skinImage sd_setImageWithURL:[NSURL URLWithString:model.skinImage]];
    [cell.skinBtnArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (idx < 3) {
            
            UIButton *Kbtn = obj;
            [Kbtn setBackgroundColor:[self getBgColorOfSelectBtnWithSelect:model.skin == Kbtn.tag]];
            [Kbtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
                if (model.skin != Kbtn.tag)
                {
                    model.skin = Kbtn.tag;
                    [self.tableView reloadData];
                }
                
            }];
        }
        
        
    }];
    

    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 52;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SDCompareDetectionHeaderView *headerview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerIden];
    [headerview setBackgroundColor:SDWhiteColor];
    NSString *time = section == 0? [self getTimeWithReportData:self.model0.data]:[self getTimeWithReportData:self.model1.data];
    [headerview.timeLab setText:time];
    
    //选择报告
    [headerview.SelectTimeBtn1 handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        self.modelSelect = section == 0? self.model0:self.model1;
        self.selectReport.selectRow = [self getSelectReportRowWithText:time];
        [self.selectReport presentTimeView];
    }];
    
    //选择报告
    [headerview.selectTimeBtn2 handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        self.modelSelect = section == 0? self.model0:self.model1;
        self.selectReport.selectRow = [self getSelectReportRowWithText:time];
        [self.selectReport presentTimeView];
    }];
    return headerview;
    
}

/**
 根据时间获取对应的报告

 @param time 报告的时间
 @return 报告对应的顺序
 */
- (NSInteger)getSelectReportRowWithText:(NSString *)time
{
    for (int t = 0; t < self.reportAllArray.count; t++) {
        SDMyReportData *data = self.reportAllArray[t];
        if ([data.createTime isEqualToString:time]) {
            return t;
        }
    }
    return 0;
}


/**
 选择报告页面
 */
- (void)addSelectReportVC
{
    self.selectReport = [SDMethodTool getViewControlWithIdentifier:@"SDCompareDetecSelectReportVC"];
    self.selectReport.dateArray = self.reportAllArray;
    @weakify(self);
    self.selectReport.selectBlock = ^(NSInteger select) {
        @strongify(self);
        //选择报告后重新刷新页面
        SDMyReportData *myReportData = self.reportAllArray[select];
        [self getReportinfoWithReportId:[myReportData.id intValue] block:^(SDDetectionReportData *data) {
        
            self.modelSelect.data = data;
            self.model0.skin = skinKind_GW;
            self.model1.skin = skinKind_GW;
            [self.tableView reloadData];
            
        }];
        
    };
    [self.selectReport.view setFrame:self.view.bounds];
    [self.view addSubview:self.selectReport.view];
    [self addChildViewController:self.selectReport];
    
}
//设置button的背景色
- (UIColor *)getBgColorOfSelectBtnWithSelect:(BOOL)sel
{
    return sel == YES? RGBA(250, 207, 92, 1):RGBA(77, 72, 74, 0.3);
}
#pragma mark - lazy加载
-(SDCompareDetectionCellModel *)model1
{
    if (!_model1) {
        _model1 = [[SDCompareDetectionCellModel alloc] init];
        _model1.skin = 1;
    }
    return _model1;
}

-(SDCompareDetectionCellModel *)model0
{
    if (!_model0) {
        _model0 = [[SDCompareDetectionCellModel alloc] init];
        _model0.skin = 1;
       
    }
    return _model0;
}
//时间转换
- (NSString *)getTimeWithReportData:(SDDetectionReportData *)data
{
    NSDateFormatter *form = [[NSDateFormatter alloc] init];
    [form setDateFormat:@"yyyy.MM.dd HH:mm"];
   return [form stringFromDate:[NSDate dateWithTimeIntervalSince1970:data.createTime/1000]];
}
- (IBAction)back:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
